package cn.edu.zafu.bigdata.zqt.UDP;

import javax.xml.crypto.Data;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Client {
    public static void main(String[] args) throws IOException {
        BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
        DatagramSocket socket=new DatagramSocket();
        InetAddress address=InetAddress.getByName("localhost");

        while (true){
            System.out.println("Enter a message to the server:");
            String sentence=reader.readLine();
            byte[] buf=sentence.getBytes();
            DatagramPacket packet=new DatagramPacket(buf,buf.length,address,9876);
            socket.send(packet);
            buf=new byte[256];
            packet=new DatagramPacket(buf,buf.length);
            socket.receive(packet);

            String received=new String(packet.getData(),0,packet.getLength());
            System.out.println("Server response:"+received);
        }
    }
}
